package util

import (
	"math/rand"
	"time"
)

// 生成 n 位随机字符串
func RandomString(n int) string {
	var letters = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
	result := make([]byte, n)
	// Go 1.20+ 不再需要显式调用 rand.Seed()
	for k := range result {
		result[k] = letters[rand.Intn(len(letters))]
	}
	return string(result)
}

// 生成随机数
func RandNum() int {
	// Go 1.20+ 不再需要显式调用 rand.Seed()
	return rand.Intn(1000) + 1000
}

func GetTimeZoneShangHai() (*time.Location, error) {
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		return nil, err
	}
	return loc, nil
}

func GetGoTime(tStr string) (*time.Time, error) {
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		return nil, err
	}
	timeObj, err := time.ParseInLocation("2006-01-02 15:04:05", tStr, loc)
	if err != nil {
		return nil, err
	}
	return &timeObj, nil
}
